package com.eric.windows.anima;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</> 2017/9/5 14:18
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLKY  Co., Ltd. Inc. All rights reserved.
 */

public class MyAnimation extends Animation {

    private float centerX;
    private float centerY;
    private int Duration;
    /**在这里camera并不是摄像头，而是空间交换工具 */
    private Camera camera  = new Camera();

    public MyAnimation(float centerX, float centerY, int duration) {
        this.centerX = centerX;
        this.centerY = centerY;
        Duration = duration;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        setDuration(Duration);
        //设置动画结束后效果保留
        setFillAfter(true);
        setInterpolator(new LinearInterpolator());

    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        camera.save();
        camera.translate(100.0f - 100.0f * interpolatedTime,150.0f * interpolatedTime - 150,80.0f - 80.0f * interpolatedTime);
        camera.rotateX(360 * interpolatedTime);
        camera.rotateY(360 * interpolatedTime);

        Matrix matrix = t.getMatrix();
        camera.getMatrix(matrix);
        matrix.preTranslate(- centerX,centerY);
        matrix.postTranslate(centerX , centerY);
        camera.restore();
    }
    

}
